#include "util/CStrTool.h"

std::string CStrTool::trim(const std::string& str) {
    std::string::size_type start = str.find_first_not_of(' ');
    if (start == std::string::npos) {
        return str;
    }
    std::string::size_type end = str.find_last_not_of(' ');
    if (end != std::string::npos) {
        return str.substr(start, end - start + 1);
    }
    return str.substr(start);
}

int CStrTool::split(const std::string& str, std::list<std::string>& ret, std::string sep) {
    if (str.empty()) {
        return 0;
    }

    std::string tmp;
    std::string::size_type pos_begin = str.find_first_not_of(sep);
    std::string::size_type sep_pos = 0;

    while (pos_begin != std::string::npos) {
        sep_pos = str.find(sep, pos_begin);
        if (sep_pos != std::string::npos) {
            tmp = str.substr(pos_begin, sep_pos - pos_begin);
            pos_begin = sep_pos + sep.length();
        } else {
            tmp = str.substr(pos_begin);
            pos_begin = sep_pos;
        }

        if (!tmp.empty()) {
            ret.push_back(trim(tmp));
            tmp.clear();
        }
    }
    return 0;
}

std::string CStrTool::replace(const std::string& str, const std::string& src, const std::string& dest) {
    std::string ret;

    std::string::size_type pos_begin = 0;
    std::string::size_type pos = str.find(src);
    while (pos != std::string::npos) {
        ret.append(str.data() + pos_begin, pos - pos_begin);
        ret += dest;
        pos_begin = pos + 1;
        pos = str.find(src, pos_begin);
    }
    if (pos_begin < str.length()) {
        ret.append(str.begin() + pos_begin, str.end());
    }
    return ret;
}

std::string CStrTool::value(std::string src, std::string sep) {
    int start = src.find_first_of(sep);
    int end = src.find_last_of(sep);
    return src.substr(start + 1, end - start - 1);
}

unsigned long long CStrTool::char2uint64(char *p) {
    unsigned long long item_id = 0;
    char *temp = p;
    while (*temp != '\0') {
        item_id = item_id * 10 + (*temp - 48);
        temp++;
    }
    return item_id;
}

void CStrTool::int2charpoint(int n, unsigned char *p) {
    if (!p) return;
    p[0] = (char)((n >> 24) & 0xff);
    p[1] = (char)((n >> 16) & 0xff);
    p[2] = (char)((n >> 8)  & 0xff);
    p[3] = (char)((n >> 0)  & 0xff);
}

long long CStrTool::charpoint2longlong(char *p) {
    long long temp = 0;
    temp = ((long long)p[0] << 56) + ((long long)p[1] << 48) + ((long long)p[2] << 40) + ((long long)p[3] << 32) +
           (p[4] << 24) + (p[5] << 16) + (p[6] << 8) + p[7];
    return temp;
}

void CStrTool::short2charpoint(short n, unsigned char *p) {
    if (!p) return;
    p[0] = (char)((n >> 8)  & 0xff);
    p[1] = (char)((n >> 0)  & 0xff);
}

unsigned int CStrTool::charpoint2int(char *p) {
    unsigned int temp = 0;
    temp = (int)(p[0] << 24) + (int)(p[1] << 16) + (int)(p[2] << 8) + p[3];
    return temp;
}

unsigned short CStrTool::charpoint2short(char *p) {
    unsigned short temp = 0;
    temp = (short)(p[0] << 8) + p[1];
    return temp;
}